package de.neua;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.StringTokenizer;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.MultiAutoCompleteTextView.Tokenizer;

public class Login extends Activity implements OnClickListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);

		// set up click listeners
		View loginButton = findViewById(R.id.loginsubmit);
		loginButton.setOnClickListener(this);
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.loginsubmit:
			System.out.println("logging in");
			String username = ((EditText) findViewById(R.id.ub_login))
					.getText().toString();
			String password = ((EditText) findViewById(R.id.ub_password))
					.getText().toString();
			HttpClient client = new DefaultHttpClient();
			HttpPost request = new HttpPost(
					"https://gw.ub.uni-freiburg.de/cgi-bin/bibdia/medk");

			// Add your data
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
			nameValuePairs.add(new BasicNameValuePair("BENUTZER", username));
			nameValuePairs.add(new BasicNameValuePair("PASSWORD", password));
			try {
				request.setEntity(new UrlEncodedFormEntity(nameValuePairs));
				BasicResponseHandler handler = new BasicResponseHandler();

				HttpResponse response = client.execute(request);
				String body = handler.handleResponse(response);
				Document doc = Jsoup.parse(body);
				int index = doc.getElementsByClass("navigation").get(0)
						.siblingIndex();
				Elements rows = doc.select("table[width=630]:has(tr:has(td>a[href*=http://])) tr");
				ArrayList<Entry> entries = new ArrayList<Entry>(10);

				// iterate over the rows leaving out the column titles row
				for (int i = 2; i < rows.size(); i++) {
					Elements cols = rows.get(i).select("td");
					boolean renewable = cols.get(0).children().size() != 0;
					String name = cols.get(1).text();
					StringTokenizer tk = new StringTokenizer(
							cols.get(2).text(), ".");
					int day = Integer.parseInt(tk.nextToken());
					int month = Integer.parseInt(tk.nextToken())-1;
					int year = Integer.parseInt(tk.nextToken());
					GregorianCalendar lendingPeriod = new GregorianCalendar(year, month, day);
					int renewed = 0;
					entries.add(new Entry(renewable, lendingPeriod, renewed,
							name));
				}

				Intent media = new Intent(this, Media.class);
				media.putParcelableArrayListExtra("entries", entries);
				startActivity(media);

			} catch (Exception e) {
				e.printStackTrace();
			}

		}

	}
}
